#!/usr/bin/python
# -*- coding: UTF-8 -*-

import client_util
from datetime import datetime
from datetime import timedelta
import config
import json
import requests
import mysql_client

datetime_parttern = "%Y%m%d%H%M00"
'''
    中国地面分钟数据
'''
def getSurfEleInRegionByTime():

    #从数据库中查询
    lastData = mysql_client.select_data("SELECT times, try_times FROM surf_ele WHERE times = (SELECT MAX(times) FROM surf_ele) LIMIT 1")
    print(lastData)
    maxTimes = 0
    tryTimes = 0
    delta = 1

    if len(lastData) == 0:
        maxTimes = config.start_times
        tryTimes = 0
    else:
        maxTimes = lastData[0][u'times']
        tryTimes = lastData[0][u'try_times']

    if tryTimes > 4:
        delta = 2

    print('before maxTimes: {}, tryTimes: {}'.format(maxTimes, tryTimes))
    lastMaxDatetime = datetime.strptime(str(maxTimes), datetime_parttern)

    startDatetime = lastMaxDatetime + timedelta(minutes=delta)
    formatted_time = startDatetime.strftime(datetime_parttern)

    print('after maxTimes: {}, tryTimes: {}'.format(maxTimes, tryTimes))
    doGetSurfEleInRegionByTime(formatted_time, tryTimes)

def initSurfEleInRegionByTime():
    '''
    初始化
    :return:
    '''

    startTime = datetime.strptime('20230101000000', datetime_parttern)
    endTime = datetime.strptime('20231031235900', datetime_parttern)

    while startTime < endTime:
        startTime = startTime + timedelta(minutes=1)
        print(startTime)
        formatted_time = startTime.strftime(datetime_parttern)
        doGetSurfEleInRegionByTime(formatted_time, 1)


def doGetSurfEleInRegionByTime(times, tryTimes):
    '''
        中国地面分钟数据
    '''

    dic = {}
    dic['interfaceId'] = 'getSurfEleInRegionByTime'
    dic['dataCode'] = 'SURF_CHN_MUL_MIN'
    dic['elements'] = 'Station_Name,NetCode,REGIONCODE,Station_levl,City,V_ACODE_4SEARCH,COUNTRYCODE,Province,Cnty,Town,Country,Town_code,D_SOURCE_ID,Admin_Code_CHN,Station_Id_C,Lat,Lon,Alti,D_RETAIN_ID,DATA_ID,IYMDHM,RYMDHM,UPDATE_TIME,Datetime,Year,Mon,Day,Hour,Min,Station_Id_d,Station_Type,REP_CORR_ID,V08010,PRS_Sensor_Alti,VIS_Sensor_Heigh,TEM_RHU_Sensor_Heigh,WIN_S_Sensor_Heigh,V02180,V02183,V02175,V02177,PRS,PRS_Sea,WIN_S_Gust_Max,WIN_D_Gust_Max,WIN_D_INST,WIN_S_INST,WIN_D_Avg_1mi,WIN_S_Avg_1mi,WIN_D_Avg_2mi,WIN_S_Avg_2mi,WIN_D_Avg_10mi,WIN_S_Avg_10mi,WIN_D_S_Max,TEM,DPT,GST_5cm,GST_10cm,GST_15cm,GST_20cm,GST_40Cm,GST_80cm,GST_160cm,GST_320cm,GST,LGST,RHU,VAP,PRE,Snow_Depth,EVP_Big,VIS_HOR_1MI,VIS_HOR_10MI,CLO_Cov,CLO_Height_LoM,NPRE_Conti_Max_EDay_Year,Q_PRS,Q_PRS_Sea,Q_WIN_D_INST,Q_WIN_S_INST,Q_WIN_D_Avg_1mi,Q_WIN_S_Avg_1mi,Q_WIN_D_Avg_2mi,Q_WIN_S_Avg_2mi,Q_WIN_D_Avg_10mi,Q_WIN_S_Avg_10mi,Q_WIN_D_S_Max,Q_TEM,Q_DPT,Q_GST_5cm,Q_GST_10cm,Q_GST_15cm,Q_GST_20cm,Q_GST_40Cm,Q_GST_80cm,Q_GST_160cm,Q_GST_320cm,Q_GST,Q_LGST,Q_RHU,Q_VAP,Q_PRE,Q_Snow_Depth,Q_VIS_HOR_1MI,Q_VIS_HOR_10MI,Q_CLO_Cov,Q_CLO_Height_LoM,RETAIN1,RETAIN2,RETAIN3,V_RETAIN4,V_RETAIN5,V_RETAIN6,V_RETAIN7,V_RETAIN8,V_RETAIN9,V_RETAIN10,PRS_Max,PRS_Max_OTime,PRS_Min,PRS_Min_OTime,TEM_Max,TEM_Max_OTime,TEM_Min,TEM_Min_OTime,WIN_S_Max,WIN_S_Max_OTime,WIN_D_INST_Max,WIN_S_Inst_Max,WIN_S_INST_Max_OTime,PRE_1h,RHU_Min,RHU_Min_OTIME,LGST_Max,LGST_Max_OTime,LGST_Min,LGST_Min_OTime,GST_Max,GST_Max_Otime,GST_Min,GST_Min_OTime,VIS_Min,VIS_Min_OTime,Q_PRS_Max,Q_PRS_Max_OTime,Q_PRS_Min,Q_PRS_Min_OTime,Q_TEM_Max,Q_TEM_Max_OTime,Q_TEM_Min,Q_TEM_Min_OTime,Q_WIN_S_Max,Q_WIN_S_Max_OTime,Q_WIN_D_INST_Max,Q_WIN_S_Inst_Max,Q_WIN_S_INST_Max_OTime,Q_PRE_1h,Q_RHU_Min,Q_RHU_Min_OTIME,Q_LGST_Max,Q_LGST_Max_OTime,Q_LGST_Min,Q_LGST_Min_OTime,Q_GST_Max,Q_GST_Max_Otime,Q_GST_Min,Q_GST_Min_OTime,Q_VIS_Min,Q_VIS_Min_OTime'
    dic['dataFormat'] = 'json'
    dic['times'] = times
    dic['adminCodes'] = config.adminCodes

    httpurl = client_util.getTqUrl(dic)
    print(httpurl)
    response = requests.get(url=httpurl)

    print('statusCode: {}'.format(response.status_code))
    if response.status_code != 200:
        insert_error(times, tryTimes)
        return

    if response.content.isspace():
        insert_error(times, tryTimes)
        return

    result = json.loads(response.content)
    returnCode = result['returnCode']
    print('resultCode: {}'.format(returnCode))

    if returnCode != '0':
        print('returnCode not available, code: {}'.format(returnCode))
        insert_error(times, tryTimes)
        return

    if result['rowCount'] == '0':
        print('rowCount is 0')
        insert_error(times, tryTimes)
        return

    DS = result['DS']
    print(type(DS))
    print(DS)

    for i in range(len(DS)):
        data = DS[i]

        tableData = {
            'times': times,
            'try_times': tryTimes,
            'Station_Name': data[u'Station_Name'].encode('utf8'),
            'Station_Name': data[u'Station_Name'].encode('utf8'),
            'NetCode': data[u'NetCode'].encode('utf8'),
            'REGIONCODE': data[u'REGIONCODE'].encode('utf8'),
            'Station_levl': data[u'Station_levl'].encode('utf8'),
            'City': data[u'City'].encode('utf8'),
            'V_ACODE_4SEARCH': data[u'V_ACODE_4SEARCH'].encode('utf8'),
            'COUNTRYCODE': data[u'COUNTRYCODE'].encode('utf8'),
            'Province': data[u'Province'].encode('utf8'),
            'Cnty': data[u'Cnty'].encode('utf8'),
            'Town': data[u'Town'].encode('utf8'),
            'Country': data[u'Country'].encode('utf8'),
            'Town_code': data[u'Town_code'].encode('utf8'),
            'D_SOURCE_ID': data[u'D_SOURCE_ID'].encode('utf8'),
            'Admin_Code_CHN': data[u'Admin_Code_CHN'].encode('utf8'),
            'Station_Id_C': data[u'Station_Id_C'].encode('utf8'),
            'Lat': data[u'Lat'].encode('utf8'),
            'Lon': data[u'Lon'].encode('utf8'),
            'Alti': data[u'Alti'].encode('utf8'),
            'D_RETAIN_ID': data[u'D_RETAIN_ID'].encode('utf8'),
            'DATA_ID': data[u'DATA_ID'].encode('utf8'),
            'IYMDHM': data[u'IYMDHM'].encode('utf8'),
            'RYMDHM': data[u'RYMDHM'].encode('utf8'),
            'UPDATE_TIME': data[u'UPDATE_TIME'].encode('utf8'),
            'Datetime': data[u'Datetime'].encode('utf8'),
            'Year': data[u'Year'].encode('utf8'),
            'Mon': data[u'Mon'].encode('utf8'),
            'Day': data[u'Day'].encode('utf8'),
            'Hour': data[u'Hour'].encode('utf8'),
            'Min': data[u'Min'].encode('utf8'),
            'Station_Id_d': data[u'Station_Id_d'].encode('utf8'),
            'Station_Type': data[u'Station_Type'].encode('utf8'),
            'REP_CORR_ID': data[u'REP_CORR_ID'].encode('utf8'),
            'V08010': data[u'V08010'].encode('utf8'),
            'PRS_Sensor_Alti': data[u'PRS_Sensor_Alti'].encode('utf8'),
            'VIS_Sensor_Heigh': data[u'VIS_Sensor_Heigh'].encode('utf8'),
            'TEM_RHU_Sensor_Heigh': data[u'TEM_RHU_Sensor_Heigh'].encode('utf8'),
            'WIN_S_Sensor_Heigh': data[u'WIN_S_Sensor_Heigh'].encode('utf8'),
            'V02180': data[u'V02180'].encode('utf8'),
            'V02183': data[u'V02183'].encode('utf8'),
            'V02175': data[u'V02175'].encode('utf8'),
            'V02177': data[u'V02177'].encode('utf8'),
            'PRS': data[u'PRS'].encode('utf8'),
            'PRS_Sea': data[u'PRS_Sea'].encode('utf8'),
            'WIN_S_Gust_Max': data[u'WIN_S_Gust_Max'].encode('utf8'),
            'WIN_D_Gust_Max': data[u'WIN_D_Gust_Max'].encode('utf8'),
            'WIN_D_INST': data[u'WIN_D_INST'].encode('utf8'),
            'WIN_S_INST': data[u'WIN_S_INST'].encode('utf8'),
            'WIN_D_Avg_1mi': data[u'WIN_D_Avg_1mi'].encode('utf8'),
            'WIN_S_Avg_1mi': data[u'WIN_S_Avg_1mi'].encode('utf8'),
            'WIN_D_Avg_2mi': data[u'WIN_D_Avg_2mi'].encode('utf8'),
            'WIN_S_Avg_2mi': data[u'WIN_S_Avg_2mi'].encode('utf8'),
            'WIN_D_Avg_10mi': data[u'WIN_D_Avg_10mi'].encode('utf8'),
            'WIN_S_Avg_10mi': data[u'WIN_S_Avg_10mi'].encode('utf8'),
            'WIN_D_S_Max': data[u'WIN_D_S_Max'].encode('utf8'),
            'TEM': data[u'TEM'].encode('utf8'),
            'DPT': data[u'DPT'].encode('utf8'),
            'GST_5cm': data[u'GST_5cm'].encode('utf8'),
            'GST_10cm': data[u'GST_10cm'].encode('utf8'),
            'GST_15cm': data[u'GST_15cm'].encode('utf8'),
            'GST_20cm': data[u'GST_20cm'].encode('utf8'),
            'GST_40Cm': data[u'GST_40Cm'].encode('utf8'),
            'GST_80cm': data[u'GST_80cm'].encode('utf8'),
            'GST_160cm': data[u'GST_160cm'].encode('utf8'),
            'GST_320cm': data[u'GST_320cm'].encode('utf8'),
            'GST': data[u'GST'].encode('utf8'),
            'LGST': data[u'LGST'].encode('utf8'),
            'RHU': data[u'RHU'].encode('utf8'),
            'VAP': data[u'VAP'].encode('utf8'),
            'PRE': data[u'PRE'].encode('utf8'),
            'Snow_Depth': data[u'Snow_Depth'].encode('utf8'),
            'EVP_Big': data[u'EVP_Big'].encode('utf8'),
            'VIS_HOR_1MI': data[u'VIS_HOR_1MI'].encode('utf8'),
            'VIS_HOR_10MI': data[u'VIS_HOR_10MI'].encode('utf8'),
            'CLO_Cov': data[u'CLO_Cov'].encode('utf8'),
            'CLO_Height_LoM': data[u'CLO_Height_LoM'].encode('utf8'),
            'NPRE_Conti_Max_EDay_Year': data[u'NPRE_Conti_Max_EDay_Year'].encode('utf8'),
            'Q_PRS': data[u'Q_PRS'].encode('utf8'),
            'Q_PRS_Sea': data[u'Q_PRS_Sea'].encode('utf8'),
            'Q_WIN_D_INST': data[u'Q_WIN_D_INST'].encode('utf8'),
            'Q_WIN_S_INST': data[u'Q_WIN_S_INST'].encode('utf8'),
            'Q_WIN_D_Avg_1mi': data[u'Q_WIN_D_Avg_1mi'].encode('utf8'),
            'Q_WIN_S_Avg_1mi': data[u'Q_WIN_S_Avg_1mi'].encode('utf8'),
            'Q_WIN_D_Avg_2mi': data[u'Q_WIN_D_Avg_2mi'].encode('utf8'),
            'Q_WIN_S_Avg_2mi': data[u'Q_WIN_S_Avg_2mi'].encode('utf8'),
            'Q_WIN_D_Avg_10mi': data[u'Q_WIN_D_Avg_10mi'].encode('utf8'),
            'Q_WIN_S_Avg_10mi': data[u'Q_WIN_S_Avg_10mi'].encode('utf8'),
            'Q_WIN_D_S_Max': data[u'Q_WIN_D_S_Max'].encode('utf8'),
            'Q_TEM': data[u'Q_TEM'].encode('utf8'),
            'Q_DPT': data[u'Q_DPT'].encode('utf8'),
            'Q_GST_5cm': data[u'Q_GST_5cm'].encode('utf8'),
            'Q_GST_10cm': data[u'Q_GST_10cm'].encode('utf8'),
            'Q_GST_15cm': data[u'Q_GST_15cm'].encode('utf8'),
            'Q_GST_20cm': data[u'Q_GST_20cm'].encode('utf8'),
            'Q_GST_40Cm': data[u'Q_GST_40Cm'].encode('utf8'),
            'Q_GST_80cm': data[u'Q_GST_80cm'].encode('utf8'),
            'Q_GST_160cm': data[u'Q_GST_160cm'].encode('utf8'),
            'Q_GST_320cm': data[u'Q_GST_320cm'].encode('utf8'),
            'Q_GST': data[u'Q_GST'].encode('utf8'),
            'Q_LGST': data[u'Q_LGST'].encode('utf8'),
            'Q_RHU': data[u'Q_RHU'].encode('utf8'),
            'Q_VAP': data[u'Q_VAP'].encode('utf8'),
            'Q_PRE': data[u'Q_PRE'].encode('utf8'),
            'Q_Snow_Depth': data[u'Q_Snow_Depth'].encode('utf8'),
            'Q_VIS_HOR_1MI': data[u'Q_VIS_HOR_1MI'].encode('utf8'),
            'Q_VIS_HOR_10MI': data[u'Q_VIS_HOR_10MI'].encode('utf8'),
            'Q_CLO_Cov': data[u'Q_CLO_Cov'].encode('utf8'),
            'Q_CLO_Height_LoM': data[u'Q_CLO_Height_LoM'].encode('utf8'),
            'RETAIN1': data[u'RETAIN1'].encode('utf8'),
            'RETAIN2': data[u'RETAIN2'].encode('utf8'),
            'RETAIN3': data[u'RETAIN3'].encode('utf8'),
            'V_RETAIN4': data[u'V_RETAIN4'].encode('utf8'),
            'V_RETAIN5': data[u'V_RETAIN5'].encode('utf8'),
            'V_RETAIN6': data[u'V_RETAIN6'].encode('utf8'),
            'V_RETAIN7': data[u'V_RETAIN7'].encode('utf8'),
            'V_RETAIN8': data[u'V_RETAIN8'].encode('utf8'),
            'V_RETAIN9': data[u'V_RETAIN9'].encode('utf8'),
            'V_RETAIN10': data[u'V_RETAIN10'].encode('utf8'),
            'PRS_Max': data[u'PRS_Max'].encode('utf8'),
            'PRS_Max_OTime': data[u'PRS_Max_OTime'].encode('utf8'),
            'PRS_Min': data[u'PRS_Min'].encode('utf8'),
            'PRS_Min_OTime': data[u'PRS_Min_OTime'].encode('utf8'),
            'TEM_Max': data[u'TEM_Max'].encode('utf8'),
            'TEM_Max_OTime': data[u'TEM_Max_OTime'].encode('utf8'),
            'TEM_Min': data[u'TEM_Min'].encode('utf8'),
            'TEM_Min_OTime': data[u'TEM_Min_OTime'].encode('utf8'),
            'WIN_S_Max': data[u'WIN_S_Max'].encode('utf8'),
            'WIN_S_Max_OTime': data[u'WIN_S_Max_OTime'].encode('utf8'),
            'WIN_D_INST_Max': data[u'WIN_D_INST_Max'].encode('utf8'),
            'WIN_S_Inst_Max': data[u'WIN_S_Inst_Max'].encode('utf8'),
            'WIN_S_INST_Max_OTime': data[u'WIN_S_INST_Max_OTime'].encode('utf8'),
            'PRE_1h': data[u'PRE_1h'].encode('utf8'),
            'RHU_Min': data[u'RHU_Min'].encode('utf8'),
            'RHU_Min_OTIME': data[u'RHU_Min_OTIME'].encode('utf8'),
            'LGST_Max': data[u'LGST_Max'].encode('utf8'),
            'LGST_Max_OTime': data[u'LGST_Max_OTime'].encode('utf8'),
            'LGST_Min': data[u'LGST_Min'].encode('utf8'),
            'LGST_Min_OTime': data[u'LGST_Min_OTime'].encode('utf8'),
            'GST_Max': data[u'GST_Max'].encode('utf8'),
            'GST_Max_Otime': data[u'GST_Max_Otime'].encode('utf8'),
            'GST_Min': data[u'GST_Min'].encode('utf8'),
            'GST_Min_OTime': data[u'GST_Min_OTime'].encode('utf8'),
            'VIS_Min': data[u'VIS_Min'].encode('utf8'),
            'VIS_Min_OTime': data[u'VIS_Min_OTime'].encode('utf8'),
            'Q_PRS_Max': data[u'Q_PRS_Max'].encode('utf8'),
            'Q_PRS_Max_OTime': data[u'Q_PRS_Max_OTime'].encode('utf8'),
            'Q_PRS_Min': data[u'Q_PRS_Min'].encode('utf8'),
            'Q_PRS_Min_OTime': data[u'Q_PRS_Min_OTime'].encode('utf8'),
            'Q_TEM_Max': data[u'Q_TEM_Max'].encode('utf8'),
            'Q_TEM_Max_OTime': data[u'Q_TEM_Max_OTime'].encode('utf8'),
            'Q_TEM_Min': data[u'Q_TEM_Min'].encode('utf8'),
            'Q_TEM_Min_OTime': data[u'Q_TEM_Min_OTime'].encode('utf8'),
            'Q_WIN_S_Max': data[u'Q_WIN_S_Max'].encode('utf8'),
            'Q_WIN_S_Max_OTime': data[u'Q_WIN_S_Max_OTime'].encode('utf8'),
            'Q_WIN_D_INST_Max': data[u'Q_WIN_D_INST_Max'].encode('utf8'),
            'Q_WIN_S_Inst_Max': data[u'Q_WIN_S_Inst_Max'].encode('utf8'),
            'Q_WIN_S_INST_Max_OTime': data[u'Q_WIN_S_INST_Max_OTime'].encode('utf8'),
            'Q_PRE_1h': data[u'Q_PRE_1h'].encode('utf8'),
            'Q_RHU_Min': data[u'Q_RHU_Min'].encode('utf8'),
            'Q_RHU_Min_OTIME': data[u'Q_RHU_Min_OTIME'].encode('utf8'),
            'Q_LGST_Max': data[u'Q_LGST_Max'].encode('utf8'),
            'Q_LGST_Max_OTime': data[u'Q_LGST_Max_OTime'].encode('utf8'),
            'Q_LGST_Min': data[u'Q_LGST_Min'].encode('utf8'),
            'Q_LGST_Min_OTime': data[u'Q_LGST_Min_OTime'].encode('utf8'),
            'Q_GST_Max': data[u'Q_GST_Max'].encode('utf8'),
            'Q_GST_Max_Otime': data[u'Q_GST_Max_Otime'].encode('utf8'),
            'Q_GST_Min': data[u'Q_GST_Min'].encode('utf8'),
            'Q_GST_Min_OTime': data[u'Q_GST_Min_OTime'].encode('utf8'),
            'Q_VIS_Min': data[u'Q_VIS_Min'].encode('utf8'),
            'Q_VIS_Min_OTime': data[u'Q_VIS_Min_OTime'].encode('utf8')
        }

        mysql_client.insert_data(tableData, 'surf_ele')

def insert_error(times, tryTimes):

    """

    :param times:
    :param tryTimes:
    :return:
    """

    tableData = {
        'times': times,
        'try_times': tryTimes + 1
    }

    mysql_client.insert_data(tableData, 'surf_ele')

if __name__ == "__main__":
    #getSurfEleInRegionByTime()
    initSurfEleInRegionByTime()
